package dev.generator.mbg.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

import java.util.List;

public class ModelPlugin extends PluginAdapter {

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
		FullyQualifiedJavaType type = topLevelClass.getType();
		FullyQualifiedJavaType buildJavaType = new FullyQualifiedJavaType(type+".Build");
		String buildJavaShortName = buildJavaType.getShortName();
		FullyQualifiedJavaType buildJavaTypeShort = new FullyQualifiedJavaType(buildJavaShortName);
		Method conMethod = new Method();
		conMethod.setConstructor(true);
		conMethod.setName(type.getShortName());
		conMethod.setVisibility(JavaVisibility.PUBLIC);
		conMethod.addBodyLine("super();");
		topLevelClass.addMethod(conMethod);
		Method buildMethod = new Method();
		buildMethod.setConstructor(true);
		buildMethod.setVisibility(JavaVisibility.PRIVATE);
		buildMethod.setName(type.getShortName());
		buildMethod.addParameter(new Parameter(buildJavaTypeShort, "build"));
		for (IntrospectedColumn column : columns) {
			String javaProperty = column.getJavaProperty();
			buildMethod.addBodyLine(javaProperty + " = build."+javaProperty +";");
		}
		topLevelClass.addMethod(buildMethod);
		InnerClass buildClass = new InnerClass(new FullyQualifiedJavaType(type+".Build"));
		buildClass.setVisibility(JavaVisibility.PUBLIC);
		buildClass.setStatic(true);
		boolean  notImport = true;
		for (IntrospectedColumn column : columns) {
			String javaProperty = column.getJavaProperty();
			FullyQualifiedJavaType javaPorpertyType = column.getFullyQualifiedJavaType();
			FullyQualifiedJavaType javaPorpertyTypeShort = new FullyQualifiedJavaType(javaPorpertyType.getShortName());
			if("Date".equals(javaPorpertyType.getShortName()) && notImport) {
				topLevelClass.addImportedType(javaPorpertyType);
			}
			Field field = new Field();
			field.setVisibility(JavaVisibility.PRIVATE);
			field.setType(column.getFullyQualifiedJavaType());
			field.setName(javaProperty);
			buildClass.addField(field);
			Method innerMethod = new Method(javaProperty);
			innerMethod.setVisibility(JavaVisibility.PUBLIC);
			innerMethod.setReturnType(buildJavaTypeShort);
			innerMethod.addParameter(new Parameter(javaPorpertyTypeShort, "value"));
			innerMethod.addBodyLine("this."+javaProperty+" = value;");
			innerMethod.addBodyLine("return this;");
			buildClass.addMethod(innerMethod);
		}
		Method innerMthod = new Method("build");
		innerMthod.setReturnType(new FullyQualifiedJavaType(type.getShortName()));
		innerMthod.setVisibility(JavaVisibility.PUBLIC);
		innerMthod.addBodyLine("return new " + type.getShortName()+"(this);");
		buildClass.addMethod(innerMthod);
		topLevelClass.addInnerClass(buildClass);
		return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
	}
	
	
}
